package com.fdb.efp.nls.service.impl.sx;

import java.util.Objects;

import com.fdb.efp.nls.service.facade.sx.NlsCreditInfoService;
import com.fdb.efp.nls.service.vo.queue.NlsProcessBizVO;
import com.fdb.efp.nls.service.vo.sx.NlsCreditInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.fdb.basic.framework.core.util.TimeUtil;
import com.fdb.efp.esb.common.constant.MsLoanConstant;
import com.fdb.efp.esb.service.bo.req.sx.CreditBasicInfo;
import com.fdb.efp.esb.service.bo.req.sx.CreditExpanding;
import com.fdb.efp.esb.service.bo.req.sx.CreditMessage;
import com.fdb.efp.esb.service.bo.req.sx.CreditReq;
import com.fdb.efp.esb.service.bo.resp.sx.CreditResp;
import com.fdb.efp.esb.service.facade.sx.CreditService;

/**
 * 授信审核通知接口
  * @author heyingfeng
  * @since 2019年3月12日 下午8:04:48
  * @version 1.0
 */
@Service("creditBackService")
public class CreditBackServiceImpl extends CreditSoltServiceImpl {

	private static Logger logger = LoggerFactory.getLogger(CreditBackServiceImpl.class);

	@Autowired
	@Qualifier("creditService")
	private CreditService creditService;// 授信审核通知

	@Autowired
	@Qualifier("nlsCreditInfoService")
	private NlsCreditInfoService nlsCreditInfoService;// 授信申请基本信息

	@Override
	protected void doExecute(NlsProcessBizVO nlsProcessBizVo, NlsCreditInfoVO nlsCreditInfoVO) throws Exception {
		String applySeq = nlsProcessBizVo.getApplySeq();// 流水号
		try {
			logger.info("授信申请审核通知接口，流水号:" + applySeq);
			Objects.requireNonNull(nlsCreditInfoVO);// 网贷申请信息不能为空
			CreditReq creditReq = new CreditReq();
			CreditMessage creditMessage = new CreditMessage();
			CreditBasicInfo creditBasicInfo = new CreditBasicInfo();
			CreditExpanding creditExpanding = new CreditExpanding();
			creditReq.setRqsFlg(nlsCreditInfoVO.getBdReqSn());//请求方需要保证永远唯一 响应方据此判断是否重复请求
			creditReq.setRetCd(MsLoanConstant.RetCodeEnum.SUCCESS.VALUE);
			creditReq.setTxnMd("async");//交易模式
			/** Message	业务数据 start */
			creditMessage.setSessionId(applySeq);// 交易流水号
			creditMessage.setEvTp("apply");//事务类型
			creditMessage.setEvID(nlsCreditInfoVO.getBdTransactionId());//事务ID
			creditMessage.setRqsTmstmp(TimeUtil.getTimeStampByPattern("yyyyMMddHHmmss"));//时间戳
			/** BasicInfo	基本信息 start */
			creditBasicInfo.setIDCertNo(nlsCreditInfoVO.getCertCode());//身份证号
			creditBasicInfo.setBankCardNo(nlsCreditInfoVO.getRecvAccount());//银行卡号
			creditBasicInfo.setCustNm(nlsCreditInfoVO.getCusName());//客户姓名
			creditBasicInfo.setMblNo(nlsCreditInfoVO.getIndivMobile());//手机号码
			/** BasicInfo	基本信息 end */
			/** Expanding	定制域 start */
			creditExpanding.setCrdtLmt(nlsCreditInfoVO.getApplyAmt()+"");	//授信额度
			creditExpanding.setCrdtDt(nlsCreditInfoVO.getApplyTerm());	//授信有效期
			creditExpanding.setInstRsnCd(nlsCreditInfoVO.getBdReasonCode());	//机构原因码
			creditExpanding.setInstRsnCdRmk(nlsCreditInfoVO.getBdReasonMsg());	//机构原因码说明
			creditExpanding.setInstPolcyCd(MsLoanConstant.RisCodeEnum.SUCCESS.VALUE);	//机构策略码
			/** Expanding	定制域 end */
			creditMessage.setBasicInfo(creditBasicInfo);
			creditMessage.setExpanding(creditExpanding);
			creditReq.setMessage(creditMessage);
			CreditResp creditResp = creditService.creditNotify(creditReq);// 发送请求
			Objects.requireNonNull(creditResp);
			if (!MsLoanConstant.RetCodeEnum.SUCCESS.VALUE.equals(creditResp.getRetCd()) || !"E1001".equals(creditResp.getRetCd())) {// 0：请求成功，其他编码均代表失败
				logger.error("调用授信审批结果消费接口返回信息出错");
			}
		} catch (Exception e) {
			logger.error("授信审核通知出现异常：" + e.getMessage());
//			nlsProcessBizVo.setExceptionFlag(true);
//			nlsProcessBizVo.setExceptionMsg(e);
		}
	}
}
